package elektra.plugin.environment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.common.base.Preconditions;
import com.google.inject.Guice;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Singleton;

import elektra.plugin.core.ICommand;
import elektra.plugin.core.ICommandBuilder;
import elektra.plugin.core.ICommandProcessor;
import elektra.plugin.core.IPluginLoader;
import elektra.plugin.core.IPluginProvider;

@Singleton
public class PluginEnvironment implements IPluginEnvironment {
  private Map<String,IPluginProvider> plugins = new HashMap<String, IPluginProvider>();  
  
  @Inject
  private BaseModule baseModule;
  
  private void validateProvider(IPluginProvider plugin){
		Preconditions.checkNotNull(plugin);
		Preconditions.checkNotNull(plugin.getName());
		Preconditions.checkNotNull(plugin.getPlugin());  
  }
  
  public List<IPluginProvider> getPluginProviders() {
    return new ArrayList<IPluginProvider>(plugins.values());
  }

  public IPluginProvider getPluginProvider(String name) {
    return plugins.get(name);
  }

  public void addPluginProvider(IPluginProvider plugin) {
    this.validateProvider(plugin);
    plugins.put(plugin.getName(), plugin);
  }

  @Log
  public void loadPlugin(IPluginLoader loader) {
     Injector injector=  Guice.createInjector(loader.getModule(),this.baseModule);
     for (Class<? extends IPluginProvider> clazz: loader.getPluginProviders()){
    	 IPluginProvider provider= injector.getInstance(clazz);
         this.addPluginProvider(provider);
     }
  }

  public String getName() {
    return null;
  }

  public String getTypeName() {
    return null;
  }

  public void setPluginContext(IPluginContext context) {}

  public ICommandBuilder getCommandBuilder() {
	  return null;
	
	  
  }
  public void init() {
	  
	
	   
  }

   public void shutDown() {

   }

   public ICommand getCommandFrom(String plugin) {
	   
	 return null;
   }

   public ICommand getAsyncCommandFrom(String plugin) {
	
	return null;
   }

   public ICommandBuilder getCommandBuilder(String plugin) {
	return null;
   }

   public ICommandProcessor getProcessor() {
	  return null;
   }

}
